//
// Created by zxh on 2020-4-27.
//

#include "envControlJni.h"
#include "env_ctrol_service.h"
#include "common.h"
#include "tcpServer.h"

JNIEXPORT jint JNICALL Java_com_example_envctel_environmentControl_initUsbSerialDeviceServer
        (JNIEnv *env,jobject obj,jint prototype,jint channel,jint bautrate)
{
    int ret = 0x00;
    LOGD("entry init usb serial device server\n");
    ret = init_usb_serail_device_server(prototype,channel,bautrate);
    return ret ;
}

JNIEXPORT jint JNICALL Java_com_example_envctel_environmentControl_initUsbSerialHotplugServer
        (JNIEnv *env, jobject obj)
{
    int ret = 0x00;

    ret = init_usbserial_hotplug_server();
    return ret;
}

JNIEXPORT jint JNICALL Java_com_example_envctel_environmentControl_getUsbSerialStatus
        (JNIEnv *env, jobject obj,jint channel)
{
    int ret = 0x00;

    ret = get_usb_serial_device_status(channel);
    return ret;
}

JNIEXPORT jint JNICALL Java_com_example_envctel_environmentControl_getModbusDeviceInfo
        (JNIEnv *env, jobject obj,jint devId,jint dataType,jint startAddr,jint num,jint * value)
{

    int ret = 0x00;

    int len = (*env)->GetArrayLength(env,value);
    if (len <= 0x00)
    {
        return (-1);
    }

    //取出数组中第一个元素的内存地址
    int *p = (int *)(*env)-> GetIntArrayElements(env,value,0);

    ret = get_modbus_device_Info(devId,dataType,startAddr,num,p);
    (*env)-> ReleaseIntArrayElements(env,value,(jint *)p,0);

    return ret;
}

JNIEXPORT jint JNICALL Java_com_example_envctel_environmentControl_setModbusDeviceInfo
        (JNIEnv *env, jobject obj,jint devId,jint dataType,jint startAddr,jint num,jint * value)
{

    int ret = 0x00;

    int len = (*env)->GetArrayLength(env,value);
    if (len <= 0x00)
    {
        return 0x00;
    }

    //取出数组中第一个元素的内存地址
    int *p = (int *)(*env)-> GetIntArrayElements(env,value,0);

    ret = set_modbus_device_Info(devId,dataType,startAddr,num,p);
    (*env)-> ReleaseIntArrayElements(env,value,(jint *)p,0);

    return ret;
}

JNIEXPORT jint JNICALL Java_com_example_envctel_environmentControl_curtainMotorDeviceControlInfo
        (JNIEnv *env, jobject obj,int prototype,jint devId,jint funcode,jint startAddr,jint num,jint * value)
{

    int ret = 0x00;

    int len = (*env)->GetArrayLength(env,value);
    if (len <= 0x00)
    {
        return 0x00;
    }

    //取出数组中第一个元素的内存地址
    int *p = (int *)(*env)-> GetIntArrayElements(env,value,0);

    ret = curtain_motor_device_control_info(prototype,devId,funcode,startAddr,num,p);
    (*env)-> ReleaseIntArrayElements(env,value,(jint *)p,0);

    return ret;
}
#if DEBUG_NETWORK_TCPSERVER_ENABLE == 0X01
JNIEXPORT jint JNICALL Java_com_example_envctel_environmentControl_initTCPServer
        (JNIEnv *env, jobject obj)
{
    init_TCP_server();
}

JNIEXPORT jint JNICALL Java_com_example_envctel_environmentControl_getTCPClientData
        (JNIEnv *env, jobject obj,jbyteArray * value)
{

    int ret = 0x00;

    int len = (*env)->GetArrayLength(env,value);
    if (len <= 0x00)
    {
        return (-1);
    }

    //取出数组中第一个元素的内存地址
    unsigned char *p = (unsigned char *)(*env)-> GetByteArrayElements(env,value,0);

    memcpy(p,tcpClientDataBuff,tcpClientDataBuffLen);
    (*env)-> ReleaseByteArrayElements(env,value,(unsigned char *)p,0);

    ret = tcpClientDataBuffLen;
    tcpClientDataBuffLen = 0x00;

    return (ret);
}
#endif
